import { defineStore } from 'pinia'
import { ref } from 'vue'
import req from '@/utils/request'

export const useUserStore = defineStore('big-user', () => {
    const token = ref('')
    const user = ref({})
    const setToken = (newToken) =>{
        token.value = newToken
    }
    const removeToken = () => {
        token.value = ''
    }
    const getUserInfo$ = () => {
        return new Promise((resolve,reject) => {
            req({method:'get',url:'/my/userinfo'})
                .then(res => {
                    console.log('user.js请求用户信息的响应结果：', res)
                    user.value = res.data
                    resolve(res.data)
                }).catch(err => {
                    console.log('user.js中获取用户信息时出现异常：', err)
                    reject(err)
                })
        })
    }
    const setUser = data => {
        user.value = data
    }
    const logout = () => {
        token.value = ''
        user.value = {}
    }

    return {
        token, setToken, removeToken,getUserInfo$, user, logout, setUser
    }
},{
    persist: true
})